<?php
/**
 * Created by PhpStorm.
 * User: dongkai
 * Date: 2018/3/18
 * Time: 上午12:24
 */
namespace App\Model\Protobuf;

use App\Model\Model;
use App\Model\Protobuf\LoadRole;
use AutoMsg\AddItemResult;
use AutoMsg\ChatMessage;
use AutoMsg\ChatMessageInfo;
use AutoMsg\ConnectingResult;
use AutoMsg\DealGoodsUpdate;
use AutoMsg\FruitsDataResult;
use AutoMsg\GoldChangedResult;
use AutoMsg\JoinGameResult;
use AutoMsg\LoadBuildInfo;
use AutoMsg\LoadManager;
use AutoMsg\LoadTradeData;
use AutoMsg\LoadTradeDataResult;
use AutoMsg\MailMsg;
use AutoMsg\MissionDataResult;
use AutoMsg\MissionResult;
use AutoMsg\NpcFavorabilityResult;
use AutoMsg\OnLineAwardsInfo;
use AutoMsg\RaffleFruitsResult;
use AutoMsg\RankingResult;
use AutoMsg\RefreshDealGoodsResult;
use AutoMsg\RoomResult;
use AutoMsg\UpdateShenjiaResult;
use EasySwoole\Config;
use think\Db;
use AutoMsg\RankdataListProto;
class Server  extends Model
{
    /**
     * 消息id:1057
     */
    function ConnectingResult($data,$MsgId='1057'){
        $str = hex2bin($data);
        $pt = new ConnectingResult();
        $pt->mergeFromString($str);
//        var_dump($pt->getRoleLists());
        $count = $pt->getRoleLists()->count();
        for($i=0;$i<$count;$i++){
            $Name = $pt->getRoleLists()[$i]->getName();
            $RoleId = $pt->getRoleLists()[$i]->getRoleId();
            $Sex = $pt->getRoleLists()[$i]->getSex();
            $arr[$i]['Name'] = $Name;
            $arr[$i]['RoleId'] = $RoleId;
            $arr[$i]['Sex'] = $Sex;
        }
        var_dump($arr);
        Db::table('protobuf')->where(['type'=>'server','MsgId'=>$MsgId])->update(['Common  '=>$arr]);

        return true;
    }

    /**
     * 排行榜
     * 消息id：1132
     */
    function RankingResult($data,$MsgId='1132'){
        $str = hex2bin($data);
        $pt = new RankingResult();
        $pt->mergeFromString($str);
        $count = $pt->getLoadRanking()->getRanking()->count();
        for ($i=1;$i<=$count;$i++){
            $RankdataListProto = $pt->getLoadRanking()->getRanking()[$i];
            $getRankDataProtoList = $RankdataListProto->getRankDataProtoList();
            $num = $getRankDataProtoList->count();
            for ($j=0;$j<$num;$j++){
                $Rank = $getRankDataProtoList[$j]->getRank();
                $Value = $getRankDataProtoList[$j]->getValue();
                $Value1 = $getRankDataProtoList[$j]->getValue1();
                $Value2 = $getRankDataProtoList[$j]->getValue2();
                $Value3 = $getRankDataProtoList[$j]->getValue3();
                $RoleId = $getRankDataProtoList[$j]->getRoleId();
                $Name = $getRankDataProtoList[$j]->getName();
                $Icon = $getRankDataProtoList[$j]->getIcon();
                $arr[$j]['Rank'] = $Rank;
                $arr[$j]['Value'] = $Value;
                $arr[$j]['Value1'] = $Value1;
                $arr[$j]['Value2'] = $Value2;
                $arr[$j]['Value3'] = $Value3;
                $arr[$j]['RoledId'] = $RoleId;
                $arr[$j]['Name'] = $Name;
                $arr[$j]['Icon'] = $Icon;
            }
            $rs = Db::table('protobuf')->where(['MsgId'=>$MsgId,'type'=>'server'])->update(['RankData'=>$arr]);
            if($rs){
                return true;
            }else{
                return false;
            }
        }
    }

    /**
     * 返回加载水果机物品
     * @param $data
     * @param string $MsgId 1030
     * @return bool
     */
    function FruitsDataResult($data,$MsgId='1030'){
        $str = hex2bin($data);
        $pt = new FruitsDataResult();
        $pt->mergeFromString($str);
        $LoadFruits = $pt->getLoadFruits()->getIterator();
        foreach ($LoadFruits as $i=>$loadFruit) {
            $ItemId = $loadFruit->getItemId();
            $FruitsId = $loadFruit->getFruitsId();
            $Count = $loadFruit->getCount();
            $arr[$i]['ItemId'] = $ItemId;
            $arr[$i]['FruitsId'] = $FruitsId;
            $arr[$i]['Count'] = $Count;
        }
//        var_dump($arr);
//        $count = $pt->getLoadFruits()->count();
////        var_dump($count);索引非0开始
//        for ($i=1;$i<=$count;$i++){
//            $getLoadFruits = $pt->getLoadFruits()[$i];
//            $ItemId = $getLoadFruits->getItemId();
//            $FruitsId = $getLoadFruits->getFruitsId();
//            $Count = $getLoadFruits->getCount();
//            $arr[$i]['ItemId'] = $ItemId;
//            $arr[$i]['FruitsId'] = $FruitsId;
//            $arr[$i]['Count'] = $Count;
//        }
//        var_dump($arr);die;
        $rs = Db::table('protobuf')->where(['MsgId'=>$MsgId,'type'=>'server'])->update(['LoadFruitsData'=>$arr]);
        if($rs){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 返回抽奖信息
     * @param $data
     * @param string $MsgId 1035
     */
    function RaffleFruitsResult($data,$MsgId='1035'){
        $str = hex2bin($data);
        $pt = new RaffleFruitsResult();
        $pt->mergeFromString($str);
        $ItemId = $pt->getItemId();
        var_dump($ItemId);
    }

    /**
     * NPC好感度
     * @param $data
     * @param string $MsgId 1037
     * @return bool
     */
    function NpcFavorabilityResult($data,$MsgId='1037'){
        $str = hex2bin($data);
        $pt = new NpcFavorabilityResult();
        $pt->mergeFromString($str);
        $count = $pt->getNpcFavorabilityData()->count();
        $data = $pt->getNpcFavorabilityData()->getIterator();
        foreach ($data as $datum) {
//            var_dump(  $datum->getNpcId());
            var_dump(  $datum->getCurrentFavorability());
        }
        for ($i=0;$i<$count;$i++){
            $NpcId = $pt->getNpcFavorabilityData()[$i]->getNpcId();
            $CurrentFavorability = $pt->getNpcFavorabilityData()[$i]->getCurrentFavorability();
            $FavorabilityLevel = $pt->getNpcFavorabilityData()[$i]->getFavorabilityLevel();
            $arr[$i]['NpcId'] = $NpcId;
            $arr[$i]['CurrentFavorability'] = $CurrentFavorability;
            $arr[$i]['FavorabilityLevel'] = $FavorabilityLevel;
        }
        $rs = Db::table('protobuf')->where(['MsgId'=>$MsgId,'type'=>'server'])->update(['LoadNpcFavorability'=>$arr]);
        if($rs){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 返回给客户端的房间信息
     * @param $data
     * @param string $MsgId 1042
     * @return bool
     */
    function RoomResult($data,$MsgId='1042'){
        $str = hex2bin($data);
        $pt = new RoomResult();
        $pt->mergeFromString($str);
        $RoleId = $pt->getRoleId();
        $RoomId = $pt->getRoomId();
        $count = $pt->getConfig()->count();
        for ($i=1;$i<=$count;$i++){
            $Config = $pt->getConfig()[$i];
            $arr['Config'][] = $Config;
        }
        $arr['RoleId'] = $RoleId;
        $arr['RoomId'] = $RoomId;
        $rs = Db::table('protobuf')->where(['MsgId'=>$MsgId,'type'=>'server'])->update(['RoomResult'=>$arr]);
        if($rs){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 添加道具进背包返回
     * @param $data
     * @param string $MsgId 1053
     * @return bool
     */
    function AddItemResult($data,$MsgId='1053'){
        $str = hex2bin($data);
        $pt = new AddItemResult();
        $pt->mergeFromString($str);
        $MaxCellNumber = $pt->getBagInfo()->getMaxCellNumber();
        $CurUsedCell = $pt->getBagInfo()->getCurUsedCell();
        $items = $pt->getBagInfo()->getItems()->getIterator();
        foreach ($items as $k =>$item) {
            $CurCount = $item->getCurCount();
            $OnSpace = $item->getOnSpace();
            $Id = $item->getId();
            $FurnitureId = $item->getFurnitureId();
            $Star = $item->getStar();
            $arr1[$k]['CurCount'] = $CurCount;
            $arr1[$k]['OnSpace'] = $OnSpace;
            $arr1[$k]['Id'] = $Id;
            $arr1[$k]['FurnitureId'] = $FurnitureId;
            $arr1[$k]['Star'] = $Star;
        }
        $furnitrues = $pt->getBagInfo()->getFurnitrues()->getIterator();
        foreach ($furnitrues as $k => $furnitrue) {
            $CurCount = $furnitrue->getCurCount();
            $OnSpace = $furnitrue->getOnSpace();
            $Id = $furnitrue->getId();
            $FurnitureId = $furnitrue->getFurnitureId();
            $Star = $furnitrue->getStar();
            $arr2[$k]['CurCount'] = $CurCount;
            $arr2[$k]['OnSpace'] = $OnSpace;
            $arr2[$k]['Id'] = $Id;
            $arr2[$k]['FurnitureId'] = $FurnitureId;
            $arr2[$k]['Star'] = $Star;
        }
        $rs = Db::table('protobuf')->where(['MsgId'=>$MsgId,'type'=>'server'])->update(
            ['items'=>$arr1,'Furnitrues'=>$arr2]);
        if($rs){
            return true;
        }else{
            return false;
        }
    }

    /**
     *
     * @param $data
     * @param string $MsgId 1065
     * @return bool
     */
    function GoldChangedResult($data,$MsgId='1065'){
        $str = hex2bin($data);
        $pt = new GoldChangedResult();
        $pt->mergeFromString($str);
        $Golds = $pt->getGold()->getIterator();
//        var_dump($Golds);
        foreach ($Golds as $k=> $gold) {
            $arr[$k] = $gold;
        }
        $rs = Db::table('protobuf')->where(['MsgId'=>$MsgId,'type'=>'server'])->update(['Gold'=>$arr]);
        if($rs){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 加入游戏
     * @param $data
     * @param string $MsgId 1066
     */
    function JoinGameResult($data,$MsgId='1066'){
        $str = hex2bin($data);
        $pt = new JoinGameResult();
        $pt->mergeFromString($str);
        $LoadRole = new LoadRole();
        $Gets = $pt->getAchievement()->getAchi()->getGets()->getIterator();
        $Gets = $LoadRole->index($Gets);

        $Rewards = $pt->getAchievement()->getAchi()->getRewards()->getIterator();
        $Rewards = $LoadRole->index($Rewards);

        $getCompanyInfo = $pt->getCompanyInfo();
        $CompanyInfo = new CompanyInfo();
        $CompanyInfo_list = $CompanyInfo->index($getCompanyInfo);
//
//        $DataStatistics = $pt->getDataCountResult()->getDataStatistics()->getIterator();
//        $DataStatistics_list = $CompanyInfo->repeated($DataStatistics);
//
//        $GoldChangeCount = $pt->getDayCountInfo()->getGoldChangeCount()->getIterator();
//        $GoldChangeCount_list = $CompanyInfo->repeated($GoldChangeCount);
//        $getCompanyInfo = $pt->getDepartInfoInfo();
//        $CompanyInfo_list = $CompanyInfo->LoadDepartMentInfo($getCompanyInfo);
//        $getFinanceLogInfo = $pt->getFinanceLogInfo()->getIterator();
//        $FinanceLogInfo_list = $CompanyInfo->LoadFinanceLogInfo($getFinanceLogInfo);
//        $Friends = $pt->getFriend()->getFriends()->getIterator();
//        $Friends_list = $CompanyInfo->FriendInfo($Friends);
//        $Blacks = $pt->getFriend()->getBlacks()->getIterator();
//        $Blacks_list = $CompanyInfo->FriendInfo($Blacks);
//
//        $TakeGiftList = $pt->getGiftTakeLogs()->getTakeGiftList()->getIterator();
//        $TakeGiftList_list = $CompanyInfo->repeated($TakeGiftList);
//
//        $LoadingGoldResult = $pt->getLoadingGoldResult()->getIterator();
//        $LoadingGoldResult_list = $CompanyInfo->SavingInfo($LoadingGoldResult);
//        $getLoadLeaderInfo = $pt->getLoadLeaderInfo();
//        $LoadLeaderInfo_list = $CompanyInfo->LoadLeaderInfo($getLoadLeaderInfo);
//        $getLoans = $pt->getLoadLoans()->getLoans();
//        $Loans_list = $CompanyInfo->LoansInfo($getLoans);
//
//        $LoadRole->index($pt->getLoadRoleInfo());
//
//        $LoadTradeInfo = $this->LoadTradeDataResult($pt->getLoadTradeInfo());
//        $Mails = $pt->getMails()->getIterator();
//        $Mails_list = $this->Mails($Mails);
//        $ManagerList = $pt->getManagerList()->getIterator();
//        $LoadManager_list = $this->LoadManager($ManagerList);
//
//        $MapInfo = $this->GetMapResult($pt->getMapInfo());
//        $Messages = $pt->getMessage()->getMessages()->getIterator();
//
//        $Mission = $this->Mission($pt->getMission());
//
//        $OnLineAward = $this->OnLineAwardsInfo($pt->getOnLineAwards()->getOnLineAward());
    }

    /**
     * @return mixed
     */
    function LoadTradeDataResult(){
        $LoadTradeDataResult = new LoadTradeDataResult();
        $LoadTrade = $LoadTradeDataResult->getLoadTrade()->getIterator();
        $LoadTradeData = new LoadTradeData();
        foreach ($LoadTrade as $item) {
            $FriendIds = $item->getFriendId()->getIterator();
            foreach ($FriendIds as $FriendId){
                $FriendIds[] = $FriendId;
            }
            $item->getItemCount()->getIterator();
            foreach ($LoadTradeData as $k =>$v) {
                $LoadTrade[$k] = $v;
            }

            $Money = $item->getMoney();
            $ShipType = $item->getShipType();
            $TradeDate = $item->getTradeDate();
        }
        $arr['LoadTradeData'] = ['FriendIds'=>$FriendIds,'ItemCount'=>$LoadTrade,'Money'=>$Money,'ShipType'=>$ShipType,'TradeDate'=>$TradeDate];
        return $arr;
    }

    /**
     *  邮件
     * @return array
     */
    function MailMsg($Mails)
    {
        $MailMsg = new MailMsg();
        foreach ($Mails as $mail) {
            $MailMsg->mergeFrom($mail);
            $Id = $MailMsg->getId();
            $Msg = $MailMsg->getMsg();
            $Read = $MailMsg->getRead();
            $Reward = $MailMsg->getReward();
            $SenderIcon = $MailMsg->getSenderIcon();
            $SenderId = $MailMsg->getSenderId();
            $SenderName = $MailMsg->getSenderName();
            $Title = $MailMsg->getTitle();
            $Items = $MailMsg->getItem()->getIterator();
            foreach ($Items as $k => $v) {
                $item[$k] = $v;
            }
            $arr[] = ['Id'=>$Id,'Msg'=>$Msg,'Read'=>$Read,'Reward'=>$Reward,'SenderIcon'=>$SenderIcon,
                'SenderId'=>$SenderId,'SenderName'=>$SenderName,'Title'=>$Title,'Items'=>$item];
        }

        return  $arr;
    }
    function LoadManager($LoadManager){
//        $LoadManager = new LoadManager();
        foreach ($LoadManager as $k =>$item) {
            $Id = $item->getId();
            $Name = $item->getName();
            $arr[$k] = ['Id'=>$Id,'Name'=>$Name];
        }
        return $arr;

    }

    function GetMapResult($getMapInfo){
        $LoadBuildInfo = $getMapInfo->getLoadBuildInfo()->getIterator();
//        $LoadBuildInfo = new LoadBuildInfo();
        foreach ($LoadBuildInfo as $k =>  $item) {
            $Name = $item->getName();
            $LeaderTime = $item->getLeaderTime();
            $RoleId = $item->getRoleId();
            $Level = $item->getLevel();
            $CostGold = $item->getCostGold();
            $Pos = $item->getPos();
            $Area = $item->getArea();
            $BuildId = $item->getBuildId();
            $BuildType = $item->getBuildType();
            $CompanyName = $item->getCompanyName();
            $CurExtendLv = $item->getCurExtendLv();
            $CurrSellPrice = $item->getCurrSellPrice();
            $CustomerAddtion = $item->getCustomerAddtion();
            $arr1[$k] = [
                'Name'=>$Name,'LeaderTime'=>$LeaderTime,'RoleId'=>$RoleId,
                'Level'=>$Level,'CostGold'=>$CostGold,'Pos'=>$Pos,'Area'=>$Area,
                '$BuildId'=>$BuildId,'$BuildType'=>$BuildType,'$CompanyName'=>$CompanyName,
                'CurExtendLv'=>$CurExtendLv,'CurrSellPrice'=>$CurrSellPrice,'CustomerAddtion'=>$CustomerAddtion
                ];
        }

        $LoadLandInfoDics = $getMapInfo->getLoadLandInfoDic()->getIterator();
//        $LoadBuildInfo = new LoadBuildInfo();
        foreach ($LoadLandInfoDics as $kk=>$LoadLandInfoDic){
            $CustomerAddtion = $LoadLandInfoDic->getCustomerAddtion();
            $CurrSellPrice = $LoadLandInfoDic->getCurrSellPrice();
            $CurExtendLv = $LoadLandInfoDic->getCurExtendLv();
            $CompanyName = $LoadLandInfoDic->getCompanyName();
            $BuildType = $LoadLandInfoDic->getBuildType();
            $BuildId = $LoadLandInfoDic->getBuildId();
            $Area = $LoadLandInfoDic->getArea();
            $Pos = $LoadLandInfoDic->getPos();
            $CostGold = $LoadLandInfoDic->getCostGold();
            $Level = $LoadLandInfoDic->getLevel();
            $RoleId = $LoadLandInfoDic->getRoleId();
            $LeaderTime = $LoadLandInfoDic->getLeaderTime();
            $Name  = $LoadLandInfoDic->getName();
            $arr2[$kk] = [
                'CustomerAddtion'=>$CustomerAddtion,'CurrSellPrice'=>$CurrSellPrice,'CurExtendLv'=>$CurExtendLv,
                'CompanyName'=>$CompanyName,'BuildType'=>$BuildType,'BuildId'=>$BuildId,'Area'=>$Area,'Pos'=>$Pos,
                'CostGold'=>$CostGold,'Level'=>$Level,'RoleId'=>$RoleId,'LeaderTime'=>$LeaderTime,'Name'=>$Name
                ];
        }
        return [
            'LoadBuildInfo' => $arr1,
            'LoadLandInfoDics' => $arr2
            ];
    }
    function ChatMessageInfo($Messages){
        $ChatMessageInfo = new ChatMessageInfo();
        foreach ($Messages as $message) {
            $Name = $message->getName();
            $RoleId = $message->getRoleId();
            $VIP = $message->getVIP();
            $OnlineTime = $message->getOnlineTime();
            $Icon = $message->getIcon();
            $ChatMessage = $ChatMessageInfo->getMessages()->getIterator();
            $ChatMessage = new ChatMessage();
            foreach ($ChatMessage as $kk => $item) {
                $Message = $ChatMessage->getMessage();
                $Time = $ChatMessage->getTime();
                $arr[$kk] = ['Message'=>$Message,'Time'=>$Time];
            }
            return ['$Name'=>$Name,'$RoleId'=>$RoleId,'$VIP'=>$VIP,
                '$OnlineTime'=>$OnlineTime,'$Icon=>$Icon',
                'ChatMessage'=>$arr];

        }
    }

    function Mission($Mission){
        $MissionResult = new MissionResult();
        $CreateRoad = $Mission->getCreateRoad();
        $Vitality = $Mission->getVitality();
        $Completes = $Mission->getCompletes()->getIterator();
        foreach ($Completes as $k =>$complete) {
            $arr[$k] = $complete;
        }
        $CreateRoadCount = $Mission->getCreateRoadCount()->getIterator();
        foreach ($CreateRoadCount as $kk =>$vv){
            $arr1[$kk] = $vv;
        }
        $Doings = $MissionResult->getDoings()->getIterator();
        $MissionDataResult = new MissionDataResult();
        foreach ($Doings as $k=>$item) {
            $Complete = $item->getComplete();
            $GetReward = $item->getGetReward();
            $MissId = $item->getMissId();
            $Progress = $item->getProgress()->getIterator();
            foreach ($Progress as $p){
                $arr[$k]['Progress'][] = $p;
            }
            $arr[$k]['Complete'] = $Complete;
            $arr[$k]['GetReward'] = $GetReward;
            $arr[$k]['MissId'] = $MissId;
        }
        $VitalityStatus = $MissionResult->getVitalityStatus()->getIterator();
        foreach ($VitalityStatus as $kkk =>$vvv) {
            $arr2[$kkk] = $vvv;
        }
        return ['CreateRoad'=>$CreateRoad,'Vitality'=>$Vitality,
           'Completes'=>$Completes,'Doings'=>$Doings,'VitalityStatus'=>$arr2];
    }
    function OnLineAward($OnLineAwardsInfo){
//        $OnLineAwardsInfo = new OnLineAwardsInfo();
        $RoleId = $OnLineAwardsInfo->getRoleId();
        $HowManyStages = $OnLineAwardsInfo->getHowManyStages();
        $IsOnline = $OnLineAwardsInfo->getIsOnline();
        $SectionTime = $OnLineAwardsInfo->getSectionTime();
        return ['RoleId'=>$RoleId,'HowManyStages'=>$HowManyStages,'IsOnline'=>$IsOnline,'SectionTime'=>$SectionTime];
    }

    /**
     * 身价值
     */
    function UpdateShenjiaResult($data,$MsgId='1075'){
        $str = hex2bin($data);
        $pt = new  UpdateShenjiaResult();
        $pt->mergeFromString($str);
        $SocialStatus = $pt->getSocialStatus();
        $rs = Db::table('protobuf')->where(['MsgId'=>$MsgId,'type'=>'server'])->update(['SocialStatus'=>$SocialStatus]);
        if($rs){
            return true;
        }else{
            return false;
        }
    }

    function RefreshDealGoodsResult($data,$MsgId='1083'){
        $str = hex2bin($data);
        $pt = new RefreshDealGoodsResult();
        $pt->mergeFromString($str);
        $Goods = $pt->getGoods()->getIterator();
//        $DealGoodsUpdate = new DealGoodsUpdate();
        foreach ($Goods as $k => $DealGoodsUpdate) {
            $Name = $DealGoodsUpdate->getName();
            $Id = $DealGoodsUpdate->getId();
            $GoldType = $DealGoodsUpdate->getGoldType();
            $DealId = $DealGoodsUpdate->getDealId();
            $BiddingCount = $DealGoodsUpdate->getBiddingCount();
            $BiddingName = $DealGoodsUpdate->getBiddingName();
            $BiddingRoleId = $DealGoodsUpdate->getBiddingRoleId();
            $Count = $DealGoodsUpdate->getCount();
            $CurPrice = $DealGoodsUpdate->getCurPrice();
            $DealType = $DealGoodsUpdate->getDealType();
            $Owner = $DealGoodsUpdate->getOwner();
            $UpTime = $DealGoodsUpdate->getUpTime();
            $arr[$k] = ['Name'=>$Name,'Id'=>$Id,'GoldType'=>$GoldType,'DealId'=>$DealId,
                'BiddingCount'=>BiddingCount,'BiddingName'=>$BiddingName,'BiddingRoleId'=>$BiddingRoleId,
                'Count'=>$Count,'CurPrice'=>$CurPrice,'DealType'=>$DealType,'Owner'=>$Owner,'UpTime'=>$UpTime];
        }
        var_dump($arr);
        $rs = Db::table('protobuf')->where(['MsgId'=>$MsgId,'type'=>'server'])->update(['Goods'=>$arr]);
        if($rs){
            return true;
        }else{
            return false;
        }
    }
}